package com.springboot.study.aspjectj;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author: guodong
 * @Date: 2021/12/17 11:35
 * @Version: 1.0
 * @Description:
 */
@Aspect
@Component
public class LogPrintAspect1{

    //自定义切点位置
    //把切面连接点放在我们注解上
    @Pointcut("@annotation(com.springboot.study.aspjectj.LogPrint)")
    private void controllerAspect(){}

    //自定义前置切面
    //访问controller方法前先执行的方法
    @Before(value = "controllerAspect()&&@annotation(logPrint)", argNames = "logPrint")
    public void printLog(LogPrint logPrint){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.ms");
        System.out.println(sdf.format(new Date())+" || "+ logPrint.desc());
    }


    @Around("controllerAspect()")
    public Object introcepter(ProceedingJoinPoint pjp) throws Throwable{
        System.out.println("拦截到了" + pjp.getSignature().getName() +"方法...");
        return pjp.proceed();
    }

    /**
     * 可以处理返回参数等信息
     * @param objList
     */
    @AfterReturning(returning = "objList", pointcut = "controllerAspect()")
    public void around(String objList) {
        System.out.println("-----------AfterReturning" + objList);
    }


}
